<template>
  <van-button :loading="isFollow" v-if="!isFollowed" @click="onFollow" class="follow-btn" type="info" color="#3296fa"
    round size="small" icon="plus">关注</van-button>
  <van-button :loading="isFollow" v-else @click="onFollow" class="follow-btn" round size="small">已关注
  </van-button>
</template>

<script>
import { addUserFollow, deleteUserFollow } from '@/api/user.js'
export default {
  data () {
    return {
      isFollow: false
    }
  },
  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    autId: {
      type: [Number, Object],
      required: true
    }
  },
  // 自定义v-model的解析规则
  model: {
    // 属性的解析
    prop: 'isFollowed',
    // 方法的解析
    event: 'updateFollow'
  },
  methods: {
    // 关注取关动态判断
    async onFollow () {
      this.isFollow = true
      try {
        if (this.isFollowed) {
          // 取关
          await deleteUserFollow(this.autId)
          this.$toast.success('取关成功')
        } else {
          // 关注
          await addUserFollow(this.autId)
          this.$toast.success('关注成功')
        }
        // this.isFollowed = !this.isFollowed
        this.$emit('updateFollow', !this.isFollowed)
      } catch (err) {
        if (err.response && err.response.status === 400) {
          return this.$toast('亲，不能关注自己 呦')
        }
        this.$toast('操作失败')
      }
      this.isFollow = false
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { }
}
</script>

<style scoped lang='less'>
</style>
